import _extends from "@babel/runtime/helpers/esm/extends";
import { createVNode as _createVNode } from "vue";
import { reactive, provide, defineComponent, watch, watchEffect } from 'vue';
import PropTypes from '../_util/vue-types';
import defaultRenderEmpty from './renderEmpty';
import LocaleProvider, { ANT_MARK } from '../locale-provider';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
export var configConsumerProps = ['getTargetContainer', 'getPopupContainer', 'rootPrefixCls', 'getPrefixCls', 'renderEmpty', 'csp', 'autoInsertSpaceInButton', 'locale', 'pageHeader'];
export var defaultPrefixCls = 'ls';

function getGlobalPrefixCls() {
  return globalConfigForApi.prefixCls || defaultPrefixCls;
}

var globalConfigByCom = reactive({});
var globalConfigBySet = reactive({}); // 权重最大

export var globalConfigForApi = reactive({});
watchEffect(function () {
  _extends(globalConfigForApi, globalConfigByCom, globalConfigBySet);

  globalConfigForApi.prefixCls = getGlobalPrefixCls();

  globalConfigForApi.getPrefixCls = function (suffixCls, customizePrefixCls) {
    if (customizePrefixCls) return customizePrefixCls;
    return suffixCls ? "".concat(globalConfigForApi.prefixCls, "-").concat(suffixCls) : globalConfigForApi.prefixCls;
  };

  globalConfigForApi.getRootPrefixCls = function (rootPrefixCls, customizePrefixCls) {
    // Customize rootPrefixCls is first priority
    if (rootPrefixCls) {
      return rootPrefixCls;
    } // If Global prefixCls provided, use this


    if (globalConfigForApi.prefixCls) {
      return globalConfigForApi.prefixCls;
    } // [Legacy] If customize prefixCls provided, we cut it to get the prefixCls


    if (customizePrefixCls && customizePrefixCls.includes('-')) {
      return customizePrefixCls.replace(/^(.*)-[^-]*$/, '$1');
    } // Fallback to default prefixCls


    return getGlobalPrefixCls();
  };
});
var stopWatchEffect;

var setGlobalConfig = function setGlobalConfig(params) {
  if (stopWatchEffect) {
    stopWatchEffect();
  }

  stopWatchEffect = watchEffect(function () {
    _extends(globalConfigBySet, reactive(params));
  });
};

export var globalConfig = function globalConfig() {
  return {
    getPrefixCls: function getPrefixCls(suffixCls, customizePrefixCls) {
      if (customizePrefixCls) return customizePrefixCls;
      return suffixCls ? "".concat(getGlobalPrefixCls(), "-").concat(suffixCls) : getGlobalPrefixCls();
    },
    getRootPrefixCls: function getRootPrefixCls(rootPrefixCls, customizePrefixCls) {
      // Customize rootPrefixCls is first priority
      if (rootPrefixCls) {
        return rootPrefixCls;
      } // If Global prefixCls provided, use this


      if (globalConfigForApi.prefixCls) {
        return globalConfigForApi.prefixCls;
      } // [Legacy] If customize prefixCls provided, we cut it to get the prefixCls


      if (customizePrefixCls && customizePrefixCls.includes('-')) {
        return customizePrefixCls.replace(/^(.*)-[^-]*$/, '$1');
      } // Fallback to default prefixCls


      return getGlobalPrefixCls();
    }
  };
};
export var configProviderProps = {
  getTargetContainer: {
    type: Function
  },
  getPopupContainer: {
    type: Function
  },
  prefixCls: String,
  getPrefixCls: {
    type: Function
  },
  renderEmpty: {
    type: Function
  },
  transformCellText: {
    type: Function
  },
  csp: {
    type: Object
  },
  input: {
    type: Object
  },
  autoInsertSpaceInButton: PropTypes.looseBool,
  locale: {
    type: Object
  },
  pageHeader: {
    type: Object
  },
  componentSize: {
    type: String
  },
  direction: {
    type: String
  },
  space: {
    type: Object
  },
  virtual: PropTypes.looseBool,
  dropdownMatchSelectWidth: {
    type: [Number, Boolean],
    default: true
  },
  form: {
    type: Object
  },
  // internal use
  notUpdateGlobalConfig: Boolean
};
var ConfigProvider = defineComponent({
  name: 'SConfigProvider',
  inheritAttrs: false,
  props: configProviderProps,
  setup: function setup(props, _ref) {
    var slots = _ref.slots;

    var getPrefixCls = function getPrefixCls(suffixCls, customizePrefixCls) {
      var _props$prefixCls = props.prefixCls,
          prefixCls = _props$prefixCls === void 0 ? 'ls' : _props$prefixCls;
      if (customizePrefixCls) return customizePrefixCls;
      return suffixCls ? "".concat(prefixCls, "-").concat(suffixCls) : prefixCls;
    };

    var renderEmptyComponent = function renderEmptyComponent(name) {
      var renderEmpty = props.renderEmpty || slots.renderEmpty || defaultRenderEmpty;
      return renderEmpty(name);
    };

    var getPrefixClsWrapper = function getPrefixClsWrapper(suffixCls, customizePrefixCls) {
      var prefixCls = props.prefixCls;
      if (customizePrefixCls) return customizePrefixCls;
      var mergedPrefixCls = prefixCls || getPrefixCls('');
      return suffixCls ? "".concat(mergedPrefixCls, "-").concat(suffixCls) : mergedPrefixCls;
    };

    var configProvider = reactive(_extends(_extends({}, props), {
      getPrefixCls: getPrefixClsWrapper,
      renderEmpty: renderEmptyComponent
    }));
    Object.keys(props).forEach(function (key) {
      watch(function () {
        return props[key];
      }, function () {
        configProvider[key] = props[key];
      });
    });

    if (!props.notUpdateGlobalConfig) {
      _extends(globalConfigByCom, configProvider);

      watch(configProvider, function () {
        _extends(globalConfigByCom, configProvider);
      });
    }

    provide('configProvider', configProvider);

    var renderProvider = function renderProvider(legacyLocale) {
      var _a;

      return _createVNode(LocaleProvider, {
        "locale": props.locale || legacyLocale,
        "ANT_MARK__": ANT_MARK
      }, {
        default: function _default() {
          return [(_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots)];
        }
      });
    };

    return function () {
      return _createVNode(LocaleReceiver, {
        "children": function children(_, __, legacyLocale) {
          return renderProvider(legacyLocale);
        }
      }, null);
    };
  }
});
export var defaultConfigProvider = reactive({
  getPrefixCls: function getPrefixCls(suffixCls, customizePrefixCls) {
    if (customizePrefixCls) return customizePrefixCls;
    return suffixCls ? "ls-".concat(suffixCls) : 'ls';
  },
  renderEmpty: defaultRenderEmpty,
  direction: 'ltr'
});
ConfigProvider.config = setGlobalConfig;

ConfigProvider.install = function (app) {
  app.component(ConfigProvider.name, ConfigProvider);
};

export default ConfigProvider;